<?php
/**
 * 设计师续费意愿控制器
 */
namespace Beerus\Modules\Designer\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\Member;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\model\Designer\RenewIntentionLog as mDRILog;

class RenewIntentionLogController extends Controller {
    const PAGE_SIZE = 20;

    public function index(Request $request, Content $content) {

        $aParams = $request->all();
        $iPage = $aParams['page'] ?? 1;
        $iPageSize = $aParams['page_size'] ?? self::PAGE_SIZE;
        $aCondition = [];
        if (!empty($aParams['uid'])) {
            $aCondition['uid'] = $aParams['uid'];
        }
        if (!empty($aParams['intention_level'])) {
            $aCondition['intention_level'] = $aParams['intention_level'];
        }
        if (!empty($aParams['op_time']['start'])) {
            $aCondition['add_time[>=]'] = strtotime(date('Y-m-d 00:00:00', strtotime($aParams['op_time']['start'])));
        }
        if (!empty($aParams['op_time']['end'])) {
            $aCondition['add_time[<]'] = strtotime(date('Y-m-d 24:00:00', strtotime($aParams['op_time']['end'])));
        }
        $iTotal = mDRILog::count($aCondition);
        $aCondition['ORDER'] = ['id' => 'DESC'];
        $aCondition['LIMIT'] = [
            ($iPage - 1) * $iPageSize,
            $iPageSize
        ];
        $aList = mDRILog::select('*', $aCondition);
        $aUids = array_unique(array_column($aList, 'uid'));
        $aMemberList = Member::getUserInfoByIds($aUids);
        $aNickList = array_column($aMemberList, 'nick', 'uid');
        foreach ($aList as &$aItem) {
            $aItem['nick'] = !empty($aNickList[$aItem['uid']]) ? $aNickList[$aItem['uid']] : '';
            $aItem['intention_level'] = mDRILog::INTENTION_LEVEL_LIST[$aItem['intention_level']];
            $aItem['add_time'] = date('Y-m-d H:i:s', $aItem['add_time']);
        }
        $gridModel = new pGridDataModel($aList, $iTotal, [
            'perPage' => $iPageSize
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->equal('uid', '设计师uid')->placeholder('请输入设计师uid');
                    $filter->equal('intention_level', '续费意愿')->select(mDRILog::INTENTION_LEVEL_LIST);
                    $filter->between('op_time', '操作时间')->date();
                    $filter->expand();
                });
                $grid->column('uid', '用户UID')->width(100);
                $grid->column('nick', '昵称')->width(100);
                $grid->column('intention_level', '续费意愿')->width(100);
                $grid->column('intention_reason', '意愿原因')->width(800);
                $grid->column('add_time', '操作时间')->width(200);
                $grid->column('admin_name', '操作人')->width(100);
                $grid->disableActions();
            }
        );
        $tab = new Tab();
        $tab->addLink('设计师列表', '/admin/Designer/list');
        $tab->addLink('续费意愿记录', '/admin/Designer/intentionLog', true);
        return $content
            ->header('设计师续费意愿记录')
            ->body($tab)
            ->row($gridList);
    }
}
